<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Support\Facades\Storage;

abstract class AbstractUser extends AbstractModel implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{

    use Authenticatable, Authorizable, CanResetPassword, Notifiable;

    /**
     * 是否是管理员
     *
     * @return bool
     */
    public function isAdmin()
    {
        return false;
    }

    /**
     * 是否是商户
     *
     * @return bool
     */
    public function isBusiness()
    {
        return false;
    }

    /**
     * 是否是普通会员
     *
     * @return bool
     */
    public function isUser()
    {
        return false;
    }

    /**
     * 短信通知路由
     *
     * @return mixed
     */
    public function routeNotificationForSms()
    {
        return $this->attributes['mobile'];
    }

    /**
     * 获取用户头像
     *
     * @param $value
     *
     * @return string
     */
    public function getAvatarAttribute($value)
    {
        if (!$value) {

            return asset('storage/default-avatar.jpg');

        } elseif (starts_with($value, 'http')) {

            return $value;

        } elseif (Storage::exists($value)) {

            return $value;
        }

        return asset('storage/default-avatar.jpg');
    }

    /**
     * 多态关联: 登录日志
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function loginLog()
    {
        return $this->morphMany(LoginLog::class, 'user');
    }
}